﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.IO;
using WorldGoldenPrice;
using WorldExchange;
using SJCGoldenPrice;
using WorldIndexInfo;
using System.Configuration;
namespace MarketInfo
{
    public partial class Service1 : ServiceBase
    {
        Thread TExchange = null;
        Thread TProduct = null;
        Thread TSJC = null;
        Thread TWorlIndex = null;
        public int ExchangeTimer = 6;
        public int ProductTimer = 6;
        public int SJCTimer = 6;
        public int WorldIndexTimer = 6;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            //IMService IM = new IMService();
            //IM.SendMessageGroup("alertonlines", "MzIxYWJj", "VGB Service MarketInfo khởi động lại lúc :" + DateTime.Now.ToString(), "webgroup", "huu_phu2004");

            this.ExchangeTimer = int.Parse(ConfigurationSettings.AppSettings["ExchangeTimer"].Trim());
            this.SJCTimer = int.Parse(ConfigurationSettings.AppSettings["SJCTimer"].Trim());
            this.ProductTimer = int.Parse(ConfigurationSettings.AppSettings["ProductTimer"].Trim());
            this.WorldIndexTimer = int.Parse(ConfigurationSettings.AppSettings["WorldIndexTimer"].Trim());
            if ((TExchange == null) && (ExchangeTimer > 0))
            {
                TExchange = new Thread(new ThreadStart(GetExchange));
                TExchange.Start();
            }
            if ((TSJC == null) && (SJCTimer > 0))
            {
                TSJC = new Thread(new ThreadStart(GetSJC));
                TSJC.Start();
            }

            if ((TProduct == null) && (ProductTimer > 0))
            {
                TProduct = new Thread(new ThreadStart(GetProduct));
                TProduct.Start();
            }

            if ((TWorlIndex == null) && (WorldIndexTimer > 0))
            {
                TWorlIndex = new Thread(new ThreadStart(GetWorldIndex));
                TWorlIndex.Start();
            }




        }

        protected override void OnStop()
        {
          //  IMService IM = new IMService();
            //IM.SendMessagePersonal("alertonlines", "MzIxYWJj", "VGB Service MarketInfo đã ngừng hoạt động lúc :" + DateTime.Now.ToString(), "webgroup", "huu_phu2004");


        }


        public void GetExchange()
        {
            while (true)
            {
                int RoleWrite = DatabaseService.AccessDB.CheckRoleWrite();
                if (RoleWrite == 1)
                {
                    Exchange_Factory EF = new Exchange_Factory();
                    EF.writeToDB(ConfigurationSettings.AppSettings["ClassExChange"].ToString().Trim());
                    DatabaseService.AccessDB.UpdateVGBProfile();
                }
                if (RoleWrite == 2)
                    ResertAllService();


                Thread.Sleep(this.ExchangeTimer * 1000);
            }
        }

        public void GetProduct()
        {
            while (true)
            {
                //if (DateTime.Now < new DateTime(2013, 2, 24))
                //    return;
                //int RoleWrite = DatabaseService.AccessDB.CheckRoleWrite();
                //if (RoleWrite == 1)
                //{
                    GoldenPrice_Factory EF = new GoldenPrice_Factory();
                    EF.writeToDB(ConfigurationSettings.AppSettings["ClassProduct"].ToString().Trim());
                    DatabaseService.AccessDB.UpdateVGBProfile();
                //}
             ///   if (RoleWrite == 2)//gianh duoc quyen ghi
                //    ResertAllService();

                Thread.Sleep(this.ProductTimer * 1000);
            }
           
        }

        public void GetSJC()
        {
            while (true)
            {
                //int RoleWrite = DatabaseService.AccessDB.CheckRoleWrite();
                //if (RoleWrite == 1)
                //{
                    SJC_Factory EF = new SJC_Factory();
                    EF.writeToDB(ConfigurationSettings.AppSettings["ClassSJC"].ToString().Trim());
                    DatabaseService.AccessDB.UpdateVGBProfile();
                


                Thread.Sleep(this.SJCTimer * 1000);
            }
        }

        public void GetWorldIndex()
        {
            while (true)
            {
                int RoleWrite = DatabaseService.AccessDB.CheckRoleWrite();

                if (RoleWrite == 1)
                {
                    WorldIndex_Factory WF = new WorldIndex_Factory();
                    WF.writeToDB(ConfigurationSettings.AppSettings["ClassWordIndex"].ToString().Trim());
                    DatabaseService.AccessDB.UpdateVGBProfile();
                }
                if (RoleWrite == 2)//gianh duoc quyen ghi
                    ResertAllService();
                Thread.Sleep(this.WorldIndexTimer * 1000);
            }
    
        }
        /// <summary>
        /// TRong truong hop chuyen server su ly
        /// 
        /// </summary>
        public void ResertAllService()
        {
            try
            {
                if (ProductTimer > 0)
                {
                    GoldenPrice_Factory GF = new GoldenPrice_Factory();
                    GF.writeToDB(ConfigurationSettings.AppSettings["ClassProduct"].ToString().Trim());
                }
                if (ExchangeTimer > 0)
                {
                    Exchange_Factory EF = new Exchange_Factory();
                    EF.writeToDB(ConfigurationSettings.AppSettings["ClassExChange"].ToString().Trim());
                }
                if (SJCTimer > 0)
                {
                    SJC_Factory SF = new SJC_Factory();
                    SF.writeToDB(ConfigurationSettings.AppSettings["ClassSJC"].ToString().Trim());
                }
                if (WorldIndexTimer > 0)
                {
                    WorldIndex_Factory WF = new WorldIndex_Factory();
                    WF.writeToDB(ConfigurationSettings.AppSettings["ClassWordIndex"].ToString().Trim());
                }
                DatabaseService.AccessDB.UpdateVGBProfile();
            }
            catch (Exception ex) { }
        }
    }
}